var map = null;
var level = 14;
var geocoder = null;
var icon = null;

	
function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("gmap"));
	map.setCenter(new GLatLng(10.776091, 106.702952), level);
	
	map.addControl(new GSmallZoomControl());
	map.setUIToDefault();

	geocoder = new GClientGeocoder();
	GEvent.addListener(map, "dblclick", clicked);
	//map.openInfoWindow(map.getCenter(), "Click the map!");

	
	icon = new GIcon();
	icon.image 		= "/wroupon/static/icon/home.png";
	icon.shadow 	= "/wroupon/static/icon/home-shadow.png";
	icon.iconSize = new GSize(32, 32);
	icon.shadowSize = new GSize(52, 32);
	icon.iconAnchor = new GPoint(0, 32);
	icon.infoWindowAnchor = new GPoint(14, 4);
	icon.infoShadowAnchor = new GPoint(5, 5);
	icon.transparent 	= "/wroupon/static/icon/home-trans.png";
	icon.printImage 	= "/wroupon/static/icon/home-print.png";
	icon.mozPrintImage 	= "/wroupon/static/icon/home-ff.png";
	
	return ;
	
	
	// Add 10 markers to the map at random locations
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();
	for (var i = 0; i < 10; i++) {
	  var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
							  southWest.lng() + lngSpan * Math.random());
	  map.addOverlay(new GMarker(latlng));
	}

  }
}

function clicked(overlay, latlng) {
  if (latlng) {
	geocoder.getLocations(latlng, function(addresses) {
	  if(addresses.Status.code != 200) {
		alert("Address: " + latlng.toUrlValue());
	  }
	  else {
		address = addresses.Placemark[0];
		var myHtml = address.address;
		map.openInfoWindow(latlng, myHtml);
	  }
	});
  }
}
function createMarker(point, number) {
  var marker = (point);
  var message = ["This","is","the","secret","message"];
  
  return marker;
}

function addRelay (lat, lng, id, czone, name) {
	var latlng = new GLatLng(lat,lng);
	var marker = new GMarker(latlng) ;
	marker.value = id;
	var relay = document.getElementById('relay_id') ;
	GEvent.addListener(marker, "click", function() {
		var myHtml = "<b>#" + id +"-"+czone+ "</b><br/>" + name;
		selRelay(relay, id, latlng) ;
		map.openInfoWindowHtml(latlng, myHtml);
	});
	map.addOverlay(marker);
}
function selRelay (sel, id,latlng) {
	for (i= 0; i< sel.options.length; i++){
		if (sel.options[i].value==id) {
			sel.options.selected = true; 
			sel.selectedIndex = i ;
			break;
		}
	}
	map.setCenter(latlng, level);
}
function selCenter (sel) {
	id = sel.options[sel.selectedIndex].value ;
	if (!id) return ;
	
	for (i= 0; i< relays.length; i++){
		var r =  relays[i] ; /*id, czone, name, lat, lng*/
		
		if (r  && r[0]==id) {
			map.setCenter(new GLatLng(r[3], r[4])); //, level
			break;
		}
	}
	
}

Ext.onReady(function(){
	initialize() ;		
	
	for (i= 0; i< relays.length; i++){
		var r = relays[i] ;/*id, czone, name, lat, lng*/
		if (r)	addRelay (r[3], r[4], r[0], r[1], r[2]) ;
	}
	
	//alert(address);	
	if (geocoder)
      {
      geocoder.getLatLng(address, function(point)
        {
        if (!point) {alert("Your Home: "+address + " not found");} // Adresse non connue par Google Maps
        else
          {
	
			var marker = new GMarker(point, icon); // Initialisation d'un marker
			map.addOverlay(marker); // Affichage du marker
			map.setCenter(point); //, level
			marker.openInfoWindowHtml("Your Home: "+address); // Affichage d'une bulle contenant l'adresse
          }
        });
      }
});
